package com.isoft.c2team3service2.service.impl;



import com.isoft.c2team3service2.entity.Post;


import com.isoft.c2team3service2.dto.PostWithLikeStatusDTO;
import com.isoft.c2team3service2.entity.UserDTO;
import com.isoft.c2team3service2.entity.UserDetailDTO;
import com.isoft.c2team3service2.mapper.*;
import com.isoft.c2team3service2.service.UserDetailService;
import com.isoft.c2team3service2.util.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserDetailServiceImpl implements UserDetailService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private PostLikeMapper postLikeMapper;

    @Autowired
    private UserFollowUserMapper userFollowUserMapper; // 普通用户关注Mapper

    @Autowired
    private UserFollowArtistMapper userFollowArtistMapper; // 艺人关注Mapper

    @Override
    public UserDetailDTO getUserDetailById(Integer targetUserId) {
        // 1. 校验当前用户是否登录
        UserDTO currentUser = UserHolder.getUser();
        if (currentUser == null) {
            throw new BusinessException("请先登录");
        }
        Integer currentUserId = currentUser.getId();

        // 2. 查询被查询用户的基础信息（昵称、用户类型、艺人名称）
        Map<String, Object> userBaseMap = userMapper.selectUserNickAndArtistName(targetUserId);
        if (userBaseMap == null) {
            throw new BusinessException("目标用户不存在");
        }

        // 3. 封装用户基础信息到DTO
        UserDetailDTO userDetailDTO = new UserDetailDTO();
        userDetailDTO.setUserId(targetUserId);
        userDetailDTO.setNickName((String) userBaseMap.get("nickName"));
        userDetailDTO.setUserType((Integer) userBaseMap.get("userType"));
        userDetailDTO.setArtistName((String) userBaseMap.get("artistName")); // 艺人专属

        // 4. 查询被查询用户的公开帖子列表
        List<Post> publicPosts = postMapper.selectPublicPostsByUserId(targetUserId);
        // 4.1 为每个帖子补充“当前用户点赞状态”
        List<PostWithLikeStatusDTO> postDTOList = publicPosts.stream().map(post -> {
            PostWithLikeStatusDTO postDTO = new PostWithLikeStatusDTO();
            BeanUtils.copyProperties(post, postDTO); // 复制帖子基础字段
            // 查当前用户是否点赞该帖子
            Integer likeCount = postLikeMapper.checkPostLikeStatus(currentUserId, post.getId());
            postDTO.setIsLiked(likeCount > 0); // true=已点赞
            return postDTO;
        }).collect(Collectors.toList());
        userDetailDTO.setPostList(postDTOList);

        // 5. 查询当前用户是否关注目标用户（区分普通用户/艺人）
        Integer targetUserType = (Integer) userBaseMap.get("userType");
        Boolean isFollowed;
        if (targetUserType == 1) { // 目标是艺人：查user_follow_artist表
            Integer followCount = userFollowArtistMapper.checkFollowStatus(currentUserId, targetUserId);
            isFollowed = followCount > 0;
        } else { // 目标是普通用户：查user_follow_user表
            Integer followCount = userFollowUserMapper.checkFollowStatus(currentUserId, targetUserId);
            isFollowed = followCount > 0;
        }
        userDetailDTO.setIsFollowed(isFollowed);

        return userDetailDTO;
    }
}